package cn.edu.ctbu.firstdata.helper;


import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.util.Map;

@Setter
@Getter
@Data
public class R {
    /**
     * 错误代码
     */
    private Integer code;
    /**
     * 提示信息
     */
    private String msg;
    /***具体的内容*/
    private Object data;


    public R() {
        put("code", 1);
        put("msg", "操作成功");
    }

    public static R error(){
        return error(-1, "操作失败");
    }

    public static R error(String msg){
        return error(-1, msg);
    }
    
    public static R error(Integer code, String msg){
        R r = new R();
        r.put("code", code);
        r.put("msg", msg);
        return r;
    }

    public static R ok(){
        return ok(null);
    }

    public static R ok(Map<String, Object> map){
        R r = new R();
        r.putAll(map);
        return r;
    }

    private void putAll(Map<String, Object> map) {
        R r = new R();
        r.setCode(1);
        r.setMsg(map.get("msg").toString());
        r.setData(map.get("data").toString());
    }


    public void put(String key, Object value) {
        if (key.equals("code")) {
            this.code = (Integer) value;
        }
        if (key.equals("msg")) {
            this.msg = (String) value;
        }
    }


}
